library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggmap)
## Loading required package: ggplot2
library(knitr)
library(stringr)
library(ggplot2)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-3
library(sp)
library(maptools)
## Checking rgeos availability: TRUE
library(scales)
asthma <- read.csv("https://data.ct.gov/api/views/javn-ujwr/rows.csv?accessType=DOWNLOAD", stringsAsFactors=F)


towntracts <- readOGR(dsn="maps", layer="census_tracts")
## OGR data source with driver: ESRI Shapefile 
## Source: "maps", layer: "census_tracts"
## with 833 features
## It has 14 fields
# creating a copy
towntracts_only <- towntracts

# turn the shapefile into a dataframe that can be worked on in R

towntracts <- fortify(towntracts, region="GEOID10")



townborders <- readOGR(dsn="maps", layer="ctgeo")
## OGR data source with driver: ESRI Shapefile 
## Source: "maps", layer: "ctgeo"
## with 169 features
## It has 6 fields
townborders_only <- townborders
townborders<- fortify(townborders, region="NAME10")

#names(asthma)[names(asthma) == 'Census.Tract'] <- 'id'
#asthma$id <- ifelse(nchar(asthma$Census.Tract) ==5, paste0("090010", asthma$Census.Tract), paste0("09009", asthma$Census.Tract))

tracts2towns <- read.csv("data/tracts_to_towns.csv", stringsAsFactors=F)

tracts2towns$id <- str_sub(tracts2towns$tract, 6,10 )
colnames(tracts2towns) <- c("id", "town", "Census.Tract")

asthma$Census.Tract <- as.character(asthma$Census.Tract)

asthma$Census.Tract <- ifelse(nchar(asthma$Census.Tract)==5, asthma$Census.Tract, str_sub(asthma$Census.Tract, 2,6 ))

asthma$ugh <- paste(asthma$Town, asthma$Census.Tract)
tracts2towns$ugh <- paste(tracts2towns$town, tracts2towns$Census.Tract)
tracts2towns$ugh <- str_trim(tracts2towns$ugh)

asthma <- left_join(asthma, tracts2towns, by="ugh")

asthma$id <- as.character(asthma$id)


asthma$id <- paste0("0", asthma$id)

total_map <- left_join(towntracts, asthma, by="id")
total_map$ugh <- NULL
total_map$town <- NULL
total_map$Census.Tract.y <- NULL

total_map2 <- gather(total_map, "category", "n", 10:19)

total_map2$n <- as.numeric(total_map2$n)

tm_ct <- ggplot() +
  geom_polygon(data = total_map2, aes(x=long, y=lat, group=group, fill=n), color = "black", size=0.2) +
  geom_polygon(data = townborders, aes(x=long, y=lat, group=group, fill=total), color = "black", fill=NA, size=0.5) +
  coord_map() +
  facet_wrap(~category, ncol=1) +
  scale_fill_distiller(type="seq", trans="reverse", palette = "Reds", breaks=pretty_breaks(n=10)) +
  theme_nothing(legend=TRUE) +
  labs(title="Asthma in Connecticut", fill="")
print(tm_ct)